﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using EntreeServer.Module;
using EntreeServer.Module.Configuration;

namespace ME.SDK.ConfigAPISample
{
    class Program
    {
        private static Guid _entreeFeatureId = new Guid("f36740ac-c2ae-4630-a03e-064e923229f1");

        static void Main(string[] args)
        {
            MEConfig();
        }

        static void MEConfig()
        {
            using (SPSite site = new SPSite("http://localhost/test"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //activate the entree web scoped feature
                    web.Features.Add(_entreeFeatureId, true);

                    //get an instance of the entree web config
                    EntreeWebConfig cfg = new EntreeWebConfig(web);

                    //check that Basic Plugin is available
                    if (!cfg.AvailableApplications.Any(a => a.GetType().Equals(typeof(BasicPlugin)))) return;

                    //create new instance of Basic Plugin
                    EntreeApplicationInstance appInstance = cfg.AvailableApplications.Where(a => a.GetType().Equals(typeof(BasicPlugin))).First().AddNewInstance(web);
                    BasicPlugin basicPlugin = appInstance.ApplicationInstance as BasicPlugin;

                    //set default property values
                    basicPlugin.ShowFullSiteButton = true;


                    //add lists to Basic Plugin config
                    foreach (SPList list in web.Lists.Cast<SPList>().Where(l => !l.Hidden))
                    {
                        //add a configuration node for this list
                        ListConfiguration lc = new ListConfiguration
                        {
                            Id = Guid.NewGuid(),
                            Sequence = 0,
                            ItemId = list.ID,
                            ItemRelativeUrl = list.RootFolder.ServerRelativeUrl,
                            ItemTitle = list.Title
                        };

                        //add the default view for the list
                        foreach (SPView view in list.Views.Cast<SPView>().Where(v => v.DefaultView))
                        {
                            ViewConfiguration vc = new ViewConfiguration
                            {
                                Id = Guid.NewGuid(),
                                ViewId = view.ID.ToString(),
                                Selected = true,
                                Sequence = 0,
                                ViewTitle = view.Title,
                                ViewSettings = new ListViewSettings
                                {
                                    HideNewButton = false,
                                    IndexFields = 3,
                                    RowLimit = 0
                                }
                            };

                            //user configurable properties

                            //add view config to list config
                            lc.ViewConfigurations.Add(vc);
                        }

                        //separate the lists into groups based on list type
                        if (list.BaseTemplate.Equals(SPListTemplateType.DocumentLibrary))
                        {
                            basicPlugin.HomeScreenViews[HomeScreenViewType.DocumentLibraries].ListConfigurations.Add(lc);
                        }
                        else if (list.BaseTemplate.Equals(SPListTemplateType.DiscussionBoard))
                        {
                            basicPlugin.HomeScreenViews[HomeScreenViewType.Discussions].ListConfigurations.Add(lc);
                        }
                        else if (list.BaseTemplate.Equals(SPListTemplateType.Survey))
                        {
                            basicPlugin.HomeScreenViews[HomeScreenViewType.Surveys].ListConfigurations.Add(lc);
                        }
                        else
                        {
                            basicPlugin.HomeScreenViews[HomeScreenViewType.Lists].ListConfigurations.Add(lc);
                        }

                        foreach (SPWeb subWeb in web.Webs)
                        {
                            basicPlugin.HomeScreenViews[HomeScreenViewType.Sites].ListConfigurations.Add(new ListConfiguration()
                            {
                                Id = subWeb.ID,
                                ItemId = subWeb.ID,
                                ItemTitle = subWeb.Title,
                                Sequence = 0
                                //ItemRelativeUrl = subWeb.ServerRelativeUrl,
                            });
                        }
                    }

                    //update Basic Plugin configuration settings
                    appInstance.Update(web);

                    //write output
                    Console.WriteLine("'{0}' ({1}) was successfully configured.", web.Title, web.Url);
                }
            }
        }
    }
}
